﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类join_sort。
    /// </summary>
    public class join_sort
    {
        public join_sort()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int jid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_join_sort where jid=" + jid + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 返回最大序号
        /// </summary>
        /// <returns></returns>
        public int MaxPx()
        {
            string sql = "SELECT MAX(px) FROM lt_join_sort ";
            DataTable maxdt = DataObject.Query(sql).Tables[0];
            if (maxdt.Rows[0][0].ToString() != "")
            {
                return (Int32.Parse(maxdt.Rows[0][0].ToString()) + 1);
            }
            else
            {
                return 1;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.join_sort model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_join_sort(");
            strSql.Append("jname,px");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.jname + "',");
            strSql.Append("" + model.px + "");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.join_sort model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_join_sort set ");
            strSql.Append("jname='" + model.jname + "',");
            strSql.Append("px=" + model.px + "");
            strSql.Append(" where jid=" + model.jid + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 排序上下移
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="type">上移，下移</param>
        public void PxMove(int id, string type)
        {
            StringBuilder strSql = new StringBuilder();
            if (type.Trim() == "down")
            {
                strSql.Append("update lt_join_sort set px = (select px from lt_join_sort where jid=" + id.ToString() + ") where jid = (select top 1 jid from lt_join_sort where px>(select px from lt_join_sort where jid=" + id.ToString() + ") order by px asc) ");
                strSql.Append(" update lt_join_sort set px = px+1 where jid=" + id.ToString());
            }
            else
            {
                strSql.Append("update lt_join_sort set px = (select px from lt_join_sort where jid=" + id.ToString() + ") where jid = (select top 1 jid from lt_join_sort where px<(select px from lt_join_sort where jid=" + id.ToString() + ") order by px desc) ");
                strSql.Append(" update lt_join_sort set px = px-1 where jid=" + id.ToString());
            }
            DataObject.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int jid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_join_sort ");
            strSql.Append(" where jid=" + jid);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
            {
                if (DataObject.ExecuteSql("delete lt_joins where jid in (" + jid.ToString() + ")") > 0)
                    return true;
                else
                    return false;
            }
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.join_sort GetModel(int jid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [jid],[jname],[px] ");
            strSql.Append(" from lt_join_sort ");
            strSql.Append(" where jid=" + jid);
            ltModel.Model.join_sort model = new ltModel.Model.join_sort();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.jid = jid;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.jname = ds.Tables[0].Rows[0]["jname"].ToString();
                if (ds.Tables[0].Rows[0]["px"].ToString() != "")
                {
                    model.px = int.Parse(ds.Tables[0].Rows[0]["px"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [jid],[jname],[px] ");
            strSql.Append(" FROM lt_join_sort ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        #endregion  成员方法
    }
}